package com.example.administrator.szb.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.HomeActivity;
import com.example.administrator.szb.activity.RegisterActivity;
import com.example.administrator.szb.fragments.base.BaseFragment;
import com.example.administrator.szb.tinkerutil.SampleApplicationLike;
import com.example.administrator.szb.http.HttpUtil;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.TokenUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by D N on 2017/9/5.
 * 用户登录的界面
 * 已经被新的登录界面替换
 */

public class FragmentUserLogin extends BaseFragment implements Response.Listener,Response.ErrorListener{
    private TextView userLogin,tv_register;
    private View view;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_user_login,container,false);
        initView();
        return view;
    }

    private void initView() {
        userLogin = (TextView) view.findViewById(R.id.tv_login);
        userLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                request();
                startActivity(new Intent(FragmentUserLogin.this.getActivity(), HomeActivity.class));
            }
        });

        tv_register = view.findViewById(R.id.tv_register);
        tv_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(FragmentUserLogin.this.getActivity(), RegisterActivity.class));
            }
        });
    }

    private void request() {
        Map<String ,String> map = new HashMap<String,String>();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("tel","18317722576");
            jsonObject.put("type","1");
        } catch (JSONException e) {
            e.printStackTrace();
        }
       // String str = jsonObject.toString();
        String code = null;
        code = TokenUtil.md5("szb").substring(0,8);
        map.put("tel","18317722576");
        map.put("type","1");
        map.put("code",code);
        HttpUtil.RequestStringPost(SampleApplicationLike.getQueueInstance(), Request.Method.POST, URLAddress.URLBASE,map,this,this);
    }

    @Override
    public void onErrorResponse(VolleyError error) {
        String e = error.toString();
    }

    @Override
    public void onResponse(Object response) {
        String e = response.toString();
    }
}
